package com.xinyiweidai.xinyiweidai.module.bind;

import android.os.Bundle;
import android.widget.Button;

import com.authreal.api.AuthBuilder;
import com.google.gson.Gson;
import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.okhttp.OkHttpUtils;
import com.lan.mylibrary.okhttp.callback.StringCallback;
import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.bean.CardBean;
import com.xinyiweidai.xinyiweidai.bean.CardResultBean;
import com.xinyiweidai.xinyiweidai.event.BindingEvent;
import com.xinyiweidai.xinyiweidai.util.Api;
import com.xinyiweidai.xinyiweidai.util.SpUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Request;

public class CardActivity extends BaseActivity {


    private Button btn_card;
    private String authKey = "dad09f88-dd34-4226-8111-f53d8e17a7fd";//pubKey
    private String urlNotify = null;//"https://idsafe-auth.udcredit.com/frontserver/4.2/api/order_query/pub_key/dad09f88-dd34-4226-8111-f53d8e17a7fd"


    @Override
    protected int getLayoutId() {
        return R.layout.activity_card;
    }


    @Override
    protected void initToolBar() {
        new MyTitle(this, R.drawable.ic_back, "证件认证", 0, true);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        btn_card = (Button) findViewById(R.id.btn_card);
    }


    @Override
    protected void addClickEvent() {
        btn_card.setOnClickListener(view -> checkCard());
    }


    private void checkCard() {
        String id = "demo_" + new Date().getTime();
        Map<String, String> map = new HashMap<>();
        AuthBuilder authBuilder = new AuthBuilder(id, authKey, urlNotify, s -> {
            CardBean bean = new Gson().fromJson(s, CardBean.class);
            if (bean.getRet_code().equals("000000")) {

                map.put("url_photoliving", bean.getUrl_photoliving());
                map.put("start_card", bean.getStart_card());
                map.put("result_auth", bean.getResult_auth());
                map.put("ret_code", bean.getRet_code());
                map.put("url_backcard", bean.getUrl_backcard());
                map.put("state_id", bean.getState_id());
                map.put("branch_issued", bean.getBranch_issued());
                map.put("addr_card", bean.getAddr_card());
                map.put("url_frontcard", bean.getUrl_frontcard());
                map.put("date_birthday", bean.getDate_birthday());
                map.put("id_no", bean.getId_no());
                map.put("ret_msg", bean.getRet_msg());

                JSONObject message1 = null;//json字符串
                String risk_tag = null;
                try {
                    message1 = new JSONObject(s);
                    risk_tag = message1.getString("risk_tag");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                map.put("risk_tag", risk_tag);
                map.put("flag_sex", bean.getFlag_sex());
                map.put("be_idcard", bean.getBe_idcard());
                map.put("id_name", bean.getId_name());
                map.put("uid", SpUtil.getInstance().getUserInfo().getUser_id());

                goCheck(map);
            }
        });
        authBuilder.faceAuth(CardActivity.this);
    }


    private void goCheck(Map<String, String> map) {
        showLoading("正在认证");
        OkHttpUtils.get()
                .url(Api.cardAuthentication)
                .params(map)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        dismissLoading();
                        MyToast.showError(CardActivity.this, "网络异常");
                    }

                    @Override
                    public void onResponse(String response) {
                        L.e(response);
                        dismissLoading();
                        parseData(response);
                    }
                });
    }


    private void parseData(String response) {
        CardResultBean bean = new Gson().fromJson(response, CardResultBean.class);
        if (bean.getCode() == 1) {
            MyToast.showSuccess(CardActivity.this, "认证成功");
            EventBus.getDefault().post(new BindingEvent("status_2"));
            startNewActivity(ContactsActivity.class);
            finish();
        } else {
            MyToast.showError(CardActivity.this, bean.getMsg());
        }
    }


}
